1931B - Make Equal - CodeForces Solution


greedy

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//random No
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); //use by rng()
#define tc(t) int t; cin >> t; while (t--)
#define int long long
#define ll long long
#define REP(i,a,b) for(ll i=a;i<b;++i)
#define REPI(i,a,b) for(ll i=a;i>=b;--i)
#define prec(n) fixed<<setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define all(a) (a).begin(),(a).end()
#define vi vector<int>
#define ii pair<int, int>
#define vii vector<ii>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define yes {cout<<'Y'<<'E'<<'S'<<endl; return;}
#define no {cout << 'N' << 'O' << endl; return;}
#define endl "\n"
#define Ka_chow ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define db1(x)                cout<<#x<<": "<<x<<"\n"
#define db2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<"\n"
#define db3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<"\n"
#define db4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<"\n"
#define db5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<"\n"
const int M = 1e9 + 7;
const int N = 1e6 + 7;
#define inf 1000000000000000000
vector<bool> sv(N + 1, true);
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
int prod(const int& a, const int& b)
{
	return ((ll)(a)) * b % M;
}
int sum(const int& a, const int& b)
{
	int c = a + b;
	return c >= M ? c - M : c;
}
int power(int a, int b) {
	int ans = 1;
	a = a % M;
	while (b) {
		if (b & 1) ans = (ans * 1LL * a) % M;
		a = (a * 1LL * a) % M;
		b >>= 1;
	}
	return ans;
}

void seive() {
	sv[0] = false, sv[1] = false;

	for (int i = 2; i * i <= N; ++i) {
		if (sv[i]) {
			for (int j = i * i; j <= N; j += i) sv[j] = 0;
		}
	}
}
void _print(vector<int> v) {
	for (auto &e : v) cout << e << " ";
	cout << endl;
}
vi prefixSum(vi v, bool fl) {
	vi ans;
	int sum = 0;
	if (fl) {
		for (auto &e : v) {
			sum += e;
			ans.eb(sum);
		}
	} else {
		REPI(i, v.size() - 1, 0) {
			sum += v[i];
			ans.eb(sum);
		}
	}
	return ans;
}

//32 r loop logabi 10^9 le & 64 r logabi 10^18 le

void Blue() {
	// Think Twice Code Once
	int n;
	cin >> n;
	vi a(n);
	REP(i, 0, n) cin >> a[i];
	int x = (accumulate(all(a), 0LL)) / n;
	for (auto &e : a) e -= x;
	// _print(a);
	vi v = prefixSum(a, 1);
	REP(i, 0, n) if (v[i] < 0) no;
	yes;
}

signed main() {
	Ka_chow
	// seive();
	tc(t)
	Blue();
}

/*
Only Know You ve Been High When Youre Feeling Low
Only Hate The Road When Youre Missing Home
Only Know You Love Her When You Let Her Go
 */


Comments

Submit
0 Comments
More Questions

22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem